package jgl.vajra.management.part.home.activity;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Gradient;
import com.baidu.mapapi.map.HeatMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import jgl.vajra.management.R;
import jgl.vajra.management.databinding.ActivityHeatChartBinding;
import jgl.vajra.management.part.home.contract.HeatChartContract;
import jgl.vajra.management.part.home.viewModel.HeatChartViewModel;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;

@CreateViewModel(HeatChartViewModel.class)
public class HeatChartActivity extends BaseMVVMActivity<HeatChartViewModel, ActivityHeatChartBinding> implements HeatChartContract.View {

    private BaiduMap mBaiduMap;
    private MapStatus mapBuild;

    @Override
    public int getLayoutId() {
        return R.layout.activity_heat_chart;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        mBinding.mapView.showScaleControl(false);  // 设置比例尺是否可见（true 可见/false不可见）
        mBinding.mapView.showZoomControls(false);  // 设置缩放控件是否可见（true 可见/false不可见）
        View child = mBinding.mapView.getChildAt(1); // 找到百度LOGO控件对象
        if ((child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);  // 设置child这个控件是否可见（VISIBLE 可见/INVISIBLE不可见 /GONE 完全隐藏）
        }

        mBaiduMap = mBinding.mapView.getMap();
        //开启热力图
        mBaiduMap.setBaiduHeatMapEnabled(false);


        LatLng ll = new LatLng(28.276159, 113.087089);
        mapBuild = new MapStatus.Builder()
                .target(ll)
                .zoom(18f)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapBuild);
        //改变地图状态
        mBaiduMap.animateMapStatus(mMapStatusUpdate);

        //设置渐变颜色值
        int[] DEFAULT_GRADIENT_COLORS = {Color.rgb(102, 225, 0), Color.rgb(255, 0, 0)};
        //设置渐变颜色起始值
        float[] DEFAULT_GRADIENT_START_POINTS = {0.4f, 1f};
        //构造颜色渐变对象
        Gradient gradient = new Gradient(DEFAULT_GRADIENT_COLORS, DEFAULT_GRADIENT_START_POINTS);

        //以下数据为随机生成地理位置点，开发者根据自己的实际业务，传入自有位置数据即可
        List<LatLng> randomList = new ArrayList<LatLng>();

        Random r = new Random();
        for (int i = 0; i < 500; i++) {
            // 116.220000,39.780000 116.570000,40.150000
            int rlat = r.nextInt(1370000);
            int rlng = r.nextInt(1370000);
            int lat = 28276159 + rlat;
            int lng = 113087089 + rlng;
            LatLng lls = new LatLng(lat / 1E6, lng / 1E6);
            randomList.add(lls);
        }
        //构造HeatMap
//在大量热力图数据情况下，build过程相对较慢，建议放在新建线程实现
        HeatMap mCustomHeatMap = new HeatMap.Builder()
                .data(randomList)
                .gradient(gradient)
                .build();

//在地图上添加自定义热力图
        mBaiduMap.addHeatMap(mCustomHeatMap);

        //移除热力图
        //heatMap.removeHeatMap();
    }

    @Override
    public void onResume() {
        mBinding.mapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        mBinding.mapView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        mBinding.mapView.onDestroy();
        super.onDestroy();
    }

    //回到当前位置
    @Override
    public void onReposition() {
        if (mapBuild != null) {
            MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapBuild);
            mBaiduMap.animateMapStatus(mapStatusUpdate);
        }
    }
}
